import {Injectable} from '@angular/core';
import {Api} from './api.service';
import {Router} from '@angular/router';
import {SnackService} from './snack.service';
import {MatSnackBar} from '@angular/material';
import {SnackSuccessComponent} from '../page/snack-success/snack-success.component';
import {SnackErrorComponent} from '../page/snack-error/snack-error.component';
import {Resume} from '../entity/resume';
import {AuthService} from './auth.service';

@Injectable()
export class TalentService {
  constructor(
    public api: Api,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
    public router: Router,
    public authService: AuthService
  ) {}
  //  增加简历
  addResume(resume: Resume) {
    resume.talentId = this.authService.talent.id;
    const seq = this.api.post2('talent/addResume', resume);
    // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '添加成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  //  修改简历
  editResume(resume: Resume) {
    let data = {data: JSON.stringify(resume), talentId: this.authService.talent.id};
    const seq = this.api.post('talent/editResume', data);
    // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '修改成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 查询简历列表
  findPositonList() {
    let data = {talentId: this.authService.talent.id};
    return this.api.post('talent/resumeList', data);
  }
  // 查询简历
  findPositonOne(id: number) {
    let data = {id: id};
    return this.api.post('talent/findResumeOne', data);
  }
  // 删除简历
  delResume(id: number) {
    let data = {id: id, talentId: this.authService.talent.id};
    return this.api.post('talent/delResume', data);
  }
  // 申请岗位
  apply(positionId: number, resumeId: number) {
    let data = {positionId: positionId, resumeId: resumeId};
    const seq = this.api.post('talent/apply', data);
    // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '申请成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }
  // 申请列表
  appList() {
    let data = {talentId: this.authService.talent.id};
    return this.api.post('talent/appList', data);
  }
  // 删除申请
  delApp(id: number, resumeId: number) {
    let data = {id: id, resumeId: resumeId};
    return this.api.post('talent/delApp', data);
  }
  // 查询求职者信息
  findTalentInfo(talentId) {
    let data = {talentId: talentId};
    return this.api.post('talent/findTalentInfo', data);
  }

  returnLogin() {
    this.router.navigate(['/login']);
  }
}
